#!/usr/bin/python

import logging
import util
import cfg

class LocalRegionWeekReader(util.Reader):
    def _read(self, reader):
        ds_idx = cfg.ds_idx
        for line in reader:
            if reader.line_num > 1 and cfg.filter_local(line):
                tue         = util.stof(util.get_arr_key_val(line, ds_idx.tue, ""))
                target      = util.stof(util.get_arr_key_val(line, ds_idx.target, ""))
                fre_rev     = util.stof(util.get_arr_key_val(line, ds_idx.fre_rev, ""))
                oth_rev     = util.stof(util.get_arr_key_val(line, ds_idx.oth_rev, ""))
                tar_rev     = util.stof(util.get_arr_key_val(line, ds_idx.tar_rev, ""))

                key = "%s,%s,%s" % (util.get_arr_key_val(line, ds_idx.week, ""),"", util.get_arr_key_val(line, ds_idx.local_reg, ""))
                #print("week_key: %s" % key)
                self._week_result["tue"][key]        = util.add_if(key, self._week_result["tue"],      tue  ,     0.0)
                self._week_result["target"][key]     = util.add_if(key, self._week_result["target"],   target  ,  0.0)
                self._week_result["fre_rev"][key]     = util.add_if(key, self._week_result["fre_rev"],   fre_rev  ,  0.0)
                self._week_result["oth_rev"][key]    = util.add_if(key, self._week_result["oth_rev"],  oth_rev  , 0.0)
                self._week_result["tar_rev"][key]    = util.add_if(key, self._week_result["tar_rev"],  tar_rev  , 0.0)

                # get the year "" local_reg acc_result
                key = "%s,%s,%s" % (util.get_arr_key_val(line, ds_idx.year, ""),"", util.get_arr_key_val(line, ds_idx.local_reg, ""))
                #print("acc_year_key:%s" % key)
                self._acc_year_result["tue"][key]        = util.add_if(key, self._acc_year_result["tue"],      tue  ,     0.0)
                self._acc_year_result["target"][key]     = util.add_if(key, self._acc_year_result["target"],   target  ,  0.0)
                self._acc_year_result["fre_rev"][key]     = util.add_if(key, self._acc_year_result["fre_rev"],   fre_rev  ,  0.0)
                self._acc_year_result["oth_rev"][key]    = util.add_if(key, self._acc_year_result["oth_rev"],  oth_rev  , 0.0)
                self._acc_year_result["tar_rev"][key]    = util.add_if(key, self._acc_year_result["tar_rev"],  tar_rev  , 0.0)


                # get the all struct for result
                key = "%s,%s" % ("", util.get_arr_key_val(line, ds_idx.local_reg, ""))
                #print("all_struct_key:%s" % key)
                self._all_result["tue"][key]        = util.add_if(key, self._all_result["tue"],      0.0  ,     0.0)
            #end if
        #end for


def local_region_week(data_source, year, week, result):
    print("\n----processing local_region_week ......")
    print("data_source:%s" % data_source)
    print("year:%s" % (year))
    print("week:%s" % (week))
    print("result:%s" % result)

    #0.  prepare input & output struct 
    week_rtab, acc_year_rtab, all_struct_rtab = util.read_csv(data_source, LocalRegionWeekReader())


    #2. gen result struct 
    last_year = int(year) - 1
    for key in all_struct_rtab["tue"] :
        week_key = "%s%02d,%s" % (year,week,key)
        #print("all_struct_key:current_year:%s" % week_key)
        all_struct_rtab["tue"][key] = util.stof(util.get_tab_key_val(week_rtab["tue"],week_key, ""))
        all_struct_rtab["target"][key] = util.stof(util.get_tab_key_val(week_rtab["target"],week_key, ""))
        all_struct_rtab["fre_rev"][key] = util.stof(util.get_tab_key_val(week_rtab["fre_rev"],week_key, ""))
        all_struct_rtab["oth_rev"][key] = util.stof(util.get_tab_key_val(week_rtab["oth_rev"],week_key, ""))
        all_struct_rtab["tar_rev"][key] = util.stof(util.get_tab_key_val(week_rtab["tar_rev"],week_key, ""))

        week_key = "%s%02d,%s" %(last_year, week, key)
        #print("all_struct_key:last_year:%s" % week_key)
        all_struct_rtab["last_tue"][key] = util.stof(util.get_tab_key_val(week_rtab["tue"],week_key, ""))
        all_struct_rtab["last_fre_rev"][key] = util.stof(util.get_tab_key_val(week_rtab["fre_rev"],week_key, ""))
        all_struct_rtab["last_oth_rev"][key] = util.stof(util.get_tab_key_val(week_rtab["oth_rev"],week_key, ""))

        acc_key = "%s,%s" % (year, key)
        #print("all_struct_key:current_year_week_key:%s" % week_key)
        all_struct_rtab["acc_tue"][key]     = util.stof(util.get_tab_key_val(acc_year_rtab["tue"], acc_key, ""))
        all_struct_rtab["acc_target"][key]  = util.stof(util.get_tab_key_val(acc_year_rtab["target"], acc_key, ""))
        all_struct_rtab["acc_fre_rev"][key] = util.stof(util.get_tab_key_val(acc_year_rtab["fre_rev"], acc_key, ""))
        all_struct_rtab["acc_oth_rev"][key] = util.stof(util.get_tab_key_val(acc_year_rtab["oth_rev"], acc_key, ""))
        all_struct_rtab["acc_tar_rev"][key] = util.stof(util.get_tab_key_val(acc_year_rtab["tar_rev"], acc_key, ""))

        acc_key = "%d,%s" % (last_year, key)
        #print("all_struct_key:last_year_week_key:%s" % week_key)
        all_struct_rtab["acc_last_tue"][key]    = util.stof(util.get_tab_key_val(acc_year_rtab["tue"], acc_key, ""))
        all_struct_rtab["acc_last_fre_rev"][key] = util.stof(util.get_tab_key_val(acc_year_rtab["fre_rev"], acc_key, ""))
        all_struct_rtab["acc_last_oth_rev"][key] = util.stof(util.get_tab_key_val(acc_year_rtab["oth_rev"], acc_key, ""))
        
    # format the result and write to result_file
    out_str = []
    for key in all_struct_rtab["tue"]:
        s = "%s,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f\n" %  (key,
                all_struct_rtab["tue"][key],
                all_struct_rtab["target"][key],
                #all_struct_rtab["last_tue"][key],
                all_struct_rtab["acc_tue"][key],
                all_struct_rtab["acc_target"][key],
                all_struct_rtab["acc_last_tue"][key],
                all_struct_rtab["fre_rev"][key],
                all_struct_rtab["oth_rev"][key],
                all_struct_rtab["tar_rev"][key],
                #all_struct_rtab["last_fre_rev"][key],
                #all_struct_rtab["last_oth_rev"][key],
                all_struct_rtab["acc_fre_rev"][key],
                all_struct_rtab["acc_oth_rev"][key],
                all_struct_rtab["acc_tar_rev"][key],
                all_struct_rtab["acc_last_fre_rev"][key],
                all_struct_rtab["acc_last_oth_rev"][key],
            )
        out_str.append(s)
    out_str.sort(key=str.lower)

    util.write_file(result, out_str)
    print("----local_region_week OK\n")



if __name__ == "__main__":
    data_source = "source/2024 wk10 1711.csv"
    week        = 10
    year        = 2024

    result="result/result_local_region_week_%s%02d.csv" %(year,week)
    local_region_week(data_source, year, week, result)
